const { promisify } = require("util");
const { program } = require("commander");
const { REPO_URL } = require("./constant");
const download = promisify(require("download-git-repo"));
const execCommand = require("../utils/exec-command.js");

function createProjectHandle() {
  program
    .command("create <project> [...others]")
    .description("default create vue project into a folder ,for example mycli create vue3_demo")
    .action(async function (project) {
      try {
        //1.从编写的项目模板中clone下来项目
        await download(REPO_URL, project, { clone: true });
        //2.提示
        console.log("Scaffold created successfully~");
        console.log(`cd  ${project}`);
        console.log("pnpm install");
        console.log("pnpm run dev");

        //3.自动进入文件并执行npm install npm run dev
        console.log(process.platform);
        const commandName = process.platform === "win32" ? "npm.cmd" : "npm";
        execCommand(commandName, ["install"], { cwd: `./${project}` });
        execCommand(commandName, ["run", "dev"], { cwd: `./${project}` });
      } catch (error) {
        console.log(error);
      }
    });
}

module.exports = createProjectHandle;
